﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace System.Erp.UI.Test
{
    [TestClass]
    public class ValueSelecterTest
    {
        [TestMethod]
        public void TestValueSelecter()
        {
            //假设有一个菜单
            Menu mnuDelete = new Menu();
            //默认应该是True
            Assert.AreEqual<bool>(true, mnuDelete.Enabled);

            //添加一个是否已选择行的表决器
            EnabledSelecter mustSelectOne = new EnabledSelecter();
            mnuDelete.EnabledSelecter.Items.Add(mustSelectOne);
            Assert.AreEqual<bool>(true, mnuDelete.Enabled);

            //添加一个权限表决器
            EnabledSelecter canDelete = new EnabledSelecter();
            mnuDelete.EnabledSelecter.Items.Add(canDelete);
            Assert.AreEqual<bool>(true, mnuDelete.Enabled);

            //权限又拆分为：删除动作的权限，当前行的权限
            EnabledSelecter canDeleteCurrent = new EnabledSelecter();
            canDelete.Items.Add(canDeleteCurrent);
            Assert.AreEqual<bool>(true, mnuDelete.Enabled);

            //现在当前行没有删除权限
            canDeleteCurrent.Value = false;
            Assert.AreEqual<bool>(false, mnuDelete.Enabled);

            //未选择任何行
            mustSelectOne.Value = false;
            Assert.AreEqual<bool>(false, mnuDelete.Enabled);

            //恢复
            canDeleteCurrent.Reset();
            Assert.AreEqual<bool>(false, mnuDelete.Enabled);
            mustSelectOne.Reset();
            Assert.AreEqual<bool>(true, mnuDelete.Enabled);
        }

        [TestMethod]
        public void TestValueSelecterItems()
        {
            Menu mnuDelete = new Menu();

            //添加一个是否已选择行的表决器，测试如果事先已经投票后，再添加到集合，应该会立即影响结果。
            EnabledSelecter mustSelectOne = new EnabledSelecter();
            mustSelectOne.Value = false;
            mnuDelete.EnabledSelecter.Items.Add(mustSelectOne);
            Assert.AreEqual<bool>(false, mnuDelete.Enabled);
            mustSelectOne.Reset();
            Assert.AreEqual<bool>(true, mnuDelete.Enabled);

            //添加一个权限表决器
            EnabledSelecter canDelete = new EnabledSelecter();
            mnuDelete.EnabledSelecter.Items.Add(canDelete);

            //权限又拆分为：删除动作的权限，当前行的权限
            EnabledSelecter canDeleteCurrent = new EnabledSelecter();
            canDeleteCurrent.Value = false; //这里测试跨级别的触发事件，隔了一层。
            canDelete.Items.Add(canDeleteCurrent);
            Assert.AreEqual<bool>(false, mnuDelete.Enabled);
            canDeleteCurrent.Value = true;
            Assert.AreEqual<bool>(true, mnuDelete.Enabled);

            //测试集合的替换
            EnabledSelecter newItem = new EnabledSelecter();
            newItem.Value = false;
            canDelete.Items[0] = newItem;
            Assert.AreEqual<bool>(false, mnuDelete.Enabled);

            //测试Remove
            canDelete.Items.Remove(newItem);
            Assert.AreEqual<bool>(true, mnuDelete.Enabled);

            //测试Clear
            mustSelectOne.Value = false;
            Assert.AreEqual<bool>(false, mnuDelete.Enabled);
            mnuDelete.EnabledSelecter.Items.Clear();
            Assert.AreEqual<bool>(true, mnuDelete.Enabled);
        }
    }

    public class Menu
    {
        private EnabledSelecter _enabledSelecter = new EnabledSelecter();

        public EnabledSelecter EnabledSelecter
        {
            get { return _enabledSelecter; }
            set { _enabledSelecter = value; }
        }

        public bool Enabled
        {
            get { return _enabledSelecter.Value; }
            set { _enabledSelecter.Value = value; }
        }
    }

    public class EnabledSelecter : ValueSelecter<bool>
    {
        public override bool DefaultValue
        {
            get
            {
                return true;
            }
        }
    }
}
